package com.lbh.cloudgallery.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbh.cloudgallery.entity.CartItemEntity;
import com.lbh.cloudgallery.service.CartItemService;
import com.lbh.cloudgallery.service.ProductService;
import com.xxx.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 
 *
 * @author 廖碧涵
 * @email lbh@qq.com
 * @date 2021-02-25 22:47:51
 */
@Api(tags = "购物车项")
@RestController
@RequestMapping("front/cartitem")
public class FrontCartItemController {
    @Autowired
    private CartItemService cartItemService;
    @Autowired
    private ProductService productService;


    @ApiOperation(value = "移除购物项")
    @DeleteMapping("/deleteCartItem")
    public R addCart(@RequestParam("memberId") Long memberId
            ,@RequestParam("productId") Integer productId){

        String s = cartItemService.deleteCartItem(memberId,productId);

        return R.ok().put("msg",s);
    }


    @ApiOperation(value = "查询购物车")
    @GetMapping("/getCartList")
    public R getCartList(@RequestParam Long memberId){

        //根据用户id查询购物车中的购物项
        List<CartItemEntity> cartItemEntities = cartItemService
                .list(new QueryWrapper<CartItemEntity>().eq("member_id", memberId));
        /**List<CartItemTo> cartItemToList = new ArrayList<>();
        for (int i = 0; i <cartItemEntities.size() ; i++) {

            CartItemEntity itemEntity = cartItemEntities.get(i);
            Long productId = itemEntity.getProductId();
            CartItemTo cartItemTo = new CartItemTo();
            ProductEntity productEntity = productService.getById(productId);
            String image = productEntity.getImage();
            cartItemTo.setImage(image);
            cartItemTo.setCount(itemEntity.getCount());
            cartItemTo.setCartId(itemEntity.getCartId());
            cartItemTo.setItemTotalPrice(itemEntity.getItemTotalPrice());
            cartItemTo.setProductName(itemEntity.getProductName());
            cartItemToList.add(i,cartItemTo);
        }*/

        //判断购物车中是否有购物项
        if (cartItemEntities!=null){
            return R.ok().put("data",cartItemEntities);
        }
        else {
            return R.error().put("msg","还没有加购的商品，去商城逛逛吧！");
        }
    }


    @ApiOperation(value ="由购物车去结算")
    @DeleteMapping("/toOrder")
    public R toOrder(@RequestParam Long memberId,@RequestParam Integer productId,@RequestParam Integer count) {
        String s = cartItemService.createFromCart(memberId, productId, count);
        return R.ok().put("msg", s);
    }
}
